package net.uman.web.manager;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.uman.entity.PlateUserRelations;
import net.uman.framework.modules.orm.Page;
import net.uman.framework.modules.orm.PropertyFilter;
import net.uman.framework.modules.web.struts2.Struts2Utils;
import net.uman.service.PlateUserRelationsManager;
import net.uman.web.CrudActionSupport;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 板块和版主关系Action
 * @author jinlong-dt
 *
 */
@Namespace("/manager")
public class PlateUserRelationsAction extends CrudActionSupport<PlateUserRelations>{
	
	private static final long serialVersionUID = 1L;
	public Long id;
	public PlateUserRelations entity;
	public Integer type;
	public String showTypeName;
	public Page<PlateUserRelations> pPage = new Page<PlateUserRelations>(10);
	@Autowired
	private PlateUserRelationsManager manager;
	
	public PlateUserRelations getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		if(type.equals(PlateUserRelations.NEW)){
			showTypeName = "版主审核";
		}else if(type.equals(PlateUserRelations.NORMAL)){
			showTypeName = "版主管理";
		}
		return "list";
	}

	public String getPlaterUserRelations(){
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		filters.add(new PropertyFilter("filter_EQI_type",String.valueOf(type)));
		pPage.setPageNo(pageNum);
		JSONObject jsonResult = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		pPage =manager.getPlateUserRelationsPage(pPage, filters);
		for(PlateUserRelations pur : pPage.getResult()){
			JSONObject jsonPUR = new JSONObject();
			jsonPUR.put("id",pur.getId());
			jsonPUR.put("userName",pur.getUserName());
			jsonPUR.put("plateName",pur.getBbsPlateName());
			jsonPUR.put("type",pur.getType());
			jsonArray.add(jsonPUR);
		}
		jsonResult.put("total", pPage.getTotalCount());
		jsonResult.put("rows", jsonArray);
		Struts2Utils.renderJson(jsonResult);
		return null;
	}
	
	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() throws Exception {
		manager.delete(id);
		return null;
	}

	public String comfirmed(){
		boolean is =manager.updateByComfired(id);
		if(is){
			Struts2Utils.renderText("操作成功");
		}else{
			Struts2Utils.renderText("操作失败");
		}
		return null;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			entity = manager.get(id);
		}else{
			entity = new PlateUserRelations();
		}
		
	}

}
